package com.epam.cdp.selenium2.core.runner;

import java.net.MalformedURLException;
import java.net.URL;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.remote.DesiredCapabilities;
import org.openqa.selenium.remote.RemoteWebDriver;

import com.epam.cdp.selenium2.core.common.webdriver.WebDriverType;

public class Browser
{
  private WebDriver webdriver;

  private URL driverHost;

  private WebDriverType driverType;

  private static Browser instance;

  private Browser()
  {
    // do nothing
  }

  public static Browser getInstance()
  {
    return instance == null ? instance = new Browser() : instance;
  }

  public WebDriver getWebdriver()
  {
    if( webdriver == null )
      webdriver = inittializeDriver();
    return webdriver;
  }

  private WebDriver inittializeDriver()
  {

    DesiredCapabilities desiredCapabilities = new DesiredCapabilities();
    desiredCapabilities.setBrowserName( getDriverType().getDriverName() );
    driverHost = getDriverHost();
    return new RemoteWebDriver( driverHost, desiredCapabilities );
  }

  public URL getDriverHost()
  {
    return driverHost;
  }

  public void setDriverHost( String driverHost )
  {
    try
    {
      this.driverHost = new URL( "http://" + driverHost + "/wd/hub" );
    }
    catch( MalformedURLException e )
    {
      e.printStackTrace();
    }
    ;
  }

  public WebDriverType getDriverType()
  {
    return driverType;
  }

  public void setDriverType( WebDriverType driverType )
  {
    this.driverType = driverType;
  }
}
